package com.mooral.web.action.user;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.DynaActionForm;

import com.mooral.bean.User;
import com.mooral.dataAccess.DataAccessConnection;

public class UserForm extends DynaActionForm {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	
	protected boolean nullOrBlank (String str){
		  return ((str == null) || (str.length() == 0));
	}
	
	
	public ActionErrors validate(ActionMapping mapping,
	                    HttpServletRequest request) {
	  ActionErrors errors = new ActionErrors();

	  User user = (User) get("user");
	  
	  String categoriesSelected[] = (String[])get("categoriesSelected");
	  if (categoriesSelected == null || categoriesSelected.length == 0)
		  errors.add("categoriesSelected", new ActionMessage("user.new.validate.categories"));

	  if (nullOrBlank((String)user.get("email")))
		    errors.add("user.email", new ActionMessage("user.new.validate.email"));

	  if (nullOrBlank((String)user.get("password")))
		    errors.add("user.password", new ActionMessage("user.new.validate.password"));

	  if (nullOrBlank((String)user.get("passwordConfirm")))
		    errors.add("user.passwordConfirm", new ActionMessage("user.new.validate.passwordConfirm"));

	  if (nullOrBlank((String)user.get("periodicity")))
		    errors.add("user.periodicity", new ActionMessage("user.new.validate.periodicity"));

	  
	  
	  return errors;
	 }
	
}
